//
//  MNProgressView.m
//  Audio
//
//  Created by xhl_dabaoMac on 2022/2/28.
//

#import "MNProgressView.h"

@implementation MNProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setDefault];
    }
    return self;
}
- (UILabel *)progressLabel{
    if (!_progressLabel) {
        _progressLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _progressLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _progressLabel;
}

/// 默认参数初始化
- (void)setDefault{
    self.line_width = 2;
    self.startAngle = M_PI_2;
    self.isClockwise = YES;
    self.showTips = NO;
    self.trackTintColor = UIColor.lightGrayColor;
    self.progressTintColor = UIColor.redColor;
}

/// 清除回到最初的状态
- (void)clearToInit{
    [self.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
    [self readyCompleteStartDrawing];
}

/// 参数初始化完毕，开始绘制
- (void)readyCompleteStartDrawing{
    if (self.line_width <= 0 || self.line_width > self.frame.size.width/2) return;
    [self drawCircleWithStartAngle:0 endAngle:M_PI*2 fillColor:UIColor.clearColor strokeColor:self.trackTintColor isAddLine:NO radius:self.frame.size.width/2];
}

/// 绘制弧线
/// @param startAngle 开始角度
/// @param endAngle 接受角度
/// @param fillColor 填充颜色
/// @param strokeColor 边框颜色
/// @param isAddLine 是否添加最后的直线
/// @param radius 弧线的半径
- (void)drawCircleWithStartAngle:(CGFloat)startAngle
                        endAngle:(CGFloat)endAngle
                       fillColor:(UIColor *)fillColor
                     strokeColor:(UIColor *)strokeColor
                       isAddLine:(BOOL)isAddLine
                          radius:(CGFloat)radius{
    
    CGPoint arcCenter = CGPointMake(self.frame.size.width/2, self.frame.size.width/2);
    
    //圆形路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:arcCenter
                                                        radius:radius
                                                    startAngle:startAngle
                                                      endAngle:endAngle
                                                     clockwise:self.isClockwise];
    if (isAddLine) {
        [path addLineToPoint:arcCenter];
    }
    
    //CAShapeLayer
    CAShapeLayer *shapLayer = [CAShapeLayer layer];
    shapLayer.path = path.CGPath;
    shapLayer.fillColor = fillColor.CGColor;//图形填充色
    shapLayer.strokeColor =  strokeColor.CGColor;//边线颜色
    shapLayer.lineWidth = self.line_width;
    [self.layer addSublayer:shapLayer];
    
}

/// 设置进度
/// @param progress 进度0～1
- (void)setProgress:(float)progress{
    if (progress < 0) progress = 0;
    if (progress > 1) progress = 1;
    
    if (progress < _progress) {
        [self clearToInit];
    }

    _progress = progress;
    
    float endAngle = self.isClockwise ? self.startAngle + M_PI*2*progress : self.startAngle - M_PI*2*progress;
    UIColor * fillColor = (self.type == MNProgressViewTypeCircular ? UIColor.clearColor :  self.progressTintColor);
    
    ///球形特殊处理角度
    float startAngle = self.type == MNProgressViewTypeSpherical ?  M_PI_2 - progress * M_PI : self.startAngle;
    endAngle = self.type == MNProgressViewTypeSpherical ?  M_PI_2 + progress * M_PI : endAngle;
    
    BOOL circular = (self.type == MNProgressViewTypeCircular);//是不是环形
    
    [self drawCircleWithStartAngle:startAngle
                          endAngle:endAngle
                         fillColor:fillColor
                       strokeColor:self.progressTintColor
                         isAddLine:self.type == MNProgressViewTypeSector
                            radius:(self.frame.size.width/2 - (circular ? 0:self.line_width))];
    if (self.showTips) {
        if (!self.progressLabel.superview)  [self addSubview:self.progressLabel];
        [self bringSubviewToFront:self.progressLabel];
        self.progressLabel.text = [NSString stringWithFormat:@"%0.2f%%",progress * 100];
    }
    
  
}

@end
